package org.nahtanoj.contacts.sheet;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

public class ColumnHeadings {
	private static final Logger log = Logger.getLogger(ColumnHeadings.class.getName());
	List<String>headings = new ArrayList<String>();

	public ColumnHeadings(int blanks, String... headings) {
		putBlankHeadings(blanks);
		putHeadings(headings);
	}

	// Initialise from a row on a spreadsheet
	public ColumnHeadings(Sheet s, int row) {
		String headings[][] = s.getValues(1, row, s.getColCount(), 1);
		for (int i = 0; i < headings.length; i++) {
			this.headings.add(headings[i][0]);
		}
	}

	public ColumnHeadings(String... headings) {
		putHeadings(headings);
	}

	public String get(int column) {
		if ((column != 0) && ((column - 1) < this.headings.size())) {
			return this.headings.get(column - 1);
		}
		return "";
	}

	public int get(String heading) {
		if (heading.equals("Status")) {
			return headings.size();
		}
		if (this.headings.contains(heading)) {
			for (int i = 0; i < this.headings.size(); i++) {
				if (this.headings.get(i) != null && this.headings.get(i).equals(heading)) {
					return i + 1;
				}
			}
		} else {
			log.warning(heading + " doesn't exist");
		}
		return 0;
	}

	private void putBlankHeadings(int headings) {
		for (int i = 0; i < headings; i++) {
			putHeadings("");
		}
	}

	private void putHeadings(String ... headings) {
		for (String h : headings) {
			if (h == null) {
				this.headings.add("");
			} else {
				this.headings.add(h);
			}
		}
	}
}
